import { getSongDetail } from '../../service/api-player'
import { audioContext } from '../../store/player-store'
const { screenWidth, screenHeight, statusBarHeight } = getApp().globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentSong: {},
    swiperHeight: '',
    currentSwiperId: 0,
    duration: 0,
    // currentTime: 0,//当前播放的时间
    playMode: [
      "/assets/imgs/order-play.png",
      "/assets/imgs/single-play.png",
      "/assets/imgs/random-play.png"
    ],
    opreationBtn: [
      "/assets/imgs/order-play.png",
      "/assets/imgs/pre-music.png",
      "/assets/imgs/pausing.png",
      "/assets/imgs/next-music.png",
      "/assets/imgs/music-menu.png",
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id);
    this.fetchData(options.id)
    this.setData({ swiperHeight: screenHeight - statusBarHeight - 44 })
    audioContext.stop = true
    audioContext.src = `https://music.163.com/song/medisa/outer/url?id=${options.id}.mp3`;
    console.log(audioContext, 'audioContext');
    audioContext.autoplay = true
    audioContext.onCanplay(() => {
      audioContext.play()
    })
    audioContext.onTimeUpdate((e) => {
      const currentTime = audioContext.currentTime
      this.setData({ currentTime })
      console.log(currentTime, 'onTimeUpdate');
    })



  },
  async fetchData(id) {
    const res = await getSongDetail(id)
    this.setData({ currentSong: res.songs[0] })
  },
  handleSwiper(e) {
    this.setData({ currentSwiperId: e.detail.current })
  }

})